Item = class('Item')

UsedItems = {}

UsedItems.health_stim = false

function Item:initialize(name, x, y, image, basic_desc, adv_desc, item_ref, scaleX, scaleY, offX, offY)

	self.name = name or "item"
	self.x = x or 0
	self.y = y or 0
	self.image = image or h_stim_img
	self.rotation = 0
	self.scaleX = scaleX or scale
	self.scaleY = scaleY or scale
	self.offX = offX or 0
	self.offY = offY or 0
	self.visible = true
	self.lit = false
	self.in_inv = false
	self.basic_desc = basic_desc or "default basic description"
	self.adv_desc = adv_desc or "default advanced desc"
	self.used_item_ref = item_ref or "default_item"

end


function Item:draw()
	if self.visible then
		if self.lit or SceneCompletelyLit then
			draw_light(self.image,
				dOX+((self.x-map_x)*(tile_size*scale)),
				dOY+((self.y-map_y)*(tile_size*scale)),
				self.rotation, self.scaleX, self.scaleY, self.offX, self.offY)
		else
			draw_dark(self.image,
				dOX+((self.x-map_x)*(tile_size*scale)),
				dOY+((self.y-map_y)*(tile_size*scale)),
				self.rotation, self.scaleX, self.scaleY, self.offX, self.offY)

		end
		--[[
		love.graphics.draw(self.image,
			dOX+((self.x-map_x)*(tile_size*scale)),
			dOY+((self.y-map_y)*(tile_size*scale)),
			self.rotation, self.scaleX, self.scaleY)
			]]
	end
end

function Item:set_lit_all( bLit )
	if bLit ~= nil then
		for i = 1, #map_items do
			map_items[i].lit = bLit
		end
	end
end

function Item:player_collision()

	local pX = map_x+view_center_x
	local pY = map_y+view_center_y

	local items = Item:check_collision(pX, pY)

	return items

end

function Item:check_collision(x, y)

	if map_items then
		local return_t = {}

		for i = 1, #map_items do
			if map_items[i].x == x and map_items[i].y == y then
				table.insert(return_t, map_items[i])
			end
		end

		if #return_t > 0 then
			return return_t
		else
			return nil
		end

	end
end



function Item:update(dt)
	if map_items then
		for i = #map_items, 1, -1 do
			map_items[i]:light()
		end
	end
end
--!
function Item:pickup()
	
	local id = self.id
	print("picking up .. " .. self.name)
	p_add_inventory(self)	
	table.remove(map_items, id)

	-- reassign ids in case table.remove leaves a gap
	if id ~= #map_items+1 then
		for i = id, #map_items+1 do
			if map_items[i] and i > 0 then
				map_items[i].id = i
			end
		end
	end

end

function Item:drop()
	print("dropping .. " .. self.name)
	local item = self
	p_rem_inventory(self.id)

	local pX = map_x+view_center_x
	local pY = map_y+view_center_y
	item.in_inv = false
	item.x = pX
	item.y = pY

	self:deactivate()

	local id = #map_items+1
	item.id = id
	map_items[id] = item

end

function Item:deactivate()

	print("default item deactivation")

end

function Item:light()

	-- the item is lit.. or maybe not

	if player.light == true then
		if p_light_beam_collide(dOX+((self.x-map_x)*(tile_size*scale)),
			dOY+((self.y-map_y)*(tile_size*scale))) then
			self.lit = true
		else
			self.lit = false
		end
	else
		self.lit = false
	end

end

function Item:destroy()

	if self.in_inv then
		--player.inventory[self.id] = nil
		p_rem_inventory(self.id)
	else
		--map_items[self.id] = nil
		table.remove(map_items, self.id)
	end

end

function Item:draw_all()
	--print("in items draw all")
	if map_items then
		--print(#map_items)

		for i = 1, #map_items do
			--print("drawing .. " .. map_items[i].name)
			map_items[i]:draw()

		end

	end


end

function Item:spawn_items(layout)

	if layout ~= nil then

		for i = 1, #layout do

			if layout[i].item_type == "health stim" then

				local i_c = #map_items
				local hs = HealthStim:new(layout[i].x, layout[i].y)

				hs.id = i_c+1

				map_items[i_c+1] = hs

			elseif layout[i].item_type == "mop" then
				local i_c = #map_items
				local mop = Mop:new(layout[i].x, layout[i].y)

				mop.id = i_c+1

				map_items[i_c+1] = mop

			end

		end

	end

end

function Item:use()

	print("using " .. self.name)

end

function Item:examine()
	print("player examined " .. self.name)
	--clean_msg()
	local msg_opts = {}
	msg_opts.x = center_x
	msg_opts.y = center_y
	msg_opts.name = self.name

	if UsedItems[self.used_item_ref] then		
		--msg_queue(self.adv_desc, msg_opts)
		draw_text(self.adv_desc)
	else
		--print(self.basic_desc)
		--msg_queue(self.basic_desc, msg_opts)
		draw_text(self.basic_desc)
	end
end

require('src.items.healthstim')
require('src.items.mop')





